import { Global, Module } from '@nestjs/common';
import { LogService } from './log.service';
import { utilities, WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import 'winston-daily-rotate-file';

@Global()
@Module({
  imports: [
    WinstonModule.forRoot({
      transports: [
        new winston.transports.DailyRotateFile({
          level: 'error',
          dirname: process.env.WINSTON_LOG_DIRNAME || './logs',
          filename: process.env.WINSTON_LOG_ERROR_FILENAME || 'error-%DATE%.log',
          datePattern: process.env.WINSTON_LOG_DATE_PATTERN || 'YYYY-MM-DD',
          maxSize: process.env.WINSTON_LOG_MAX_SIZE || '20m',
          maxFiles: process.env.WINSTON_LOG_ERROR_MAX_FILES || '14d',
        }),
        new winston.transports.DailyRotateFile({
          level: 'warn',
          dirname: process.env.WINSTON_LOG_DIRNAME || './logs',
          filename: process.env.WINSTON_LOG_WARN_FILENAME || 'warn-%DATE%.log',
          datePattern: process.env.WINSTON_LOG_DATE_PATTERN || 'YYYY-MM-DD',
          maxSize: process.env.WINSTON_LOG_MAX_SIZE || '20m',
          maxFiles: process.env.WINSTON_LOG_WARN_MAX_FILES || '3d',
        }),
        // 添加常规输出日志
        new winston.transports.DailyRotateFile({
          level: 'info',
          dirname: process.env.WINSTON_LOG_DIRNAME || './logs',
          filename: process.env.WINSTON_LOG_INFO_FILENAME || 'info-%DATE%.log',
          datePattern: process.env.WINSTON_LOG_DATE_PATTERN || 'YYYY-MM-DD',
          maxSize: process.env.WINSTON_LOG_MAX_SIZE || '20m',
          maxFiles: process.env.WINSTON_LOG_INFO_MAX_FILES || '3d',
        }),
        new winston.transports.Console({
          format: winston.format.combine(winston.format.timestamp(), utilities.format.nestLike()),
        }),
      ],
    }),
  ],
  providers: [LogService],
  exports: [LogService],
})
export class LogModule {}
